<?php

namespace app\admin\controller\system;

use app\admin\controller\Controller;
use app\common\controller\ResourceController;
use app\model\AdminRolePermission;
use think\App;

class AdminRole  extends Controller
{

    use ResourceController;

    public $with = ['permissions'];


    public function __construct(App $app)
    {
        parent::__construct($app);
        return $this->model = new \app\model\AdminRole();
    }


    /**
     * 分配权限
     */
    public function distribution(){

        if ($msg = dmjValidate([
            'role_id' => 'require|number',
            'permissions' => 'require'
        ],[],input())){
            return fail($msg);
        }

        $permissions = input('permissions');
        $role_id = input('role_id');
        $permissionsIds = getSelect($permissions);

        AdminRolePermission::query()
            ->where('role_id',$role_id)
            ->delete();
        foreach ($permissionsIds as $id){
            AdminRolePermission::findOrCreate([
                'role_id' => $role_id,
                'permission_id' => $id,
            ]);
        }

        return success();

    }


}